//
//  XMTabBer.m
//  Second
//
//  Created by Alex on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMTabBer.h"
@interface XMTabBer ()
@property (nonatomic,strong)XMView *tlMenuView ;
@end

@implementation XMTabBer

+ (instancetype)buttonWithTransform {
    
    static XMTabBer *tab = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        tab = [[XMTabBer alloc]init];
    });
    return tab;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        _ISShowMenuButton = NO;
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"WeChat_1468841187"] forState:UIControlStateNormal];
        publishButton.size = CGSizeMake(50, 50);
        [publishButton addTarget:self action:@selector(clickAddButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishButton];
        self.publishButton = publishButton;
        
        
        XMView *tlMenuView =[XMView standardMenuView];
        tlMenuView.centerPoint = self.publishButton.center;
        __weak typeof(self) weakSelf = self;
        tlMenuView.clickAddButton = ^(NSInteger tag, UIColor *color){
            weakSelf.backgroundColor = color;
            _ISShowMenuButton = YES;
            [weakSelf clickAddButton:publishButton];
        };
        _tlMenuView = tlMenuView;
        
        
    }
    return self;
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.publishButton.center = CGPointMake(self.width * 0.5, self.height * 0.4);
    CGFloat buttinY = 0;
    CGFloat buttinW = self.width / 5;
    CGFloat buttinH = self.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")])continue;
        
        CGFloat buttinX = buttinW * ((index > 1) ? (index + 1) : index);
        
        button.frame = CGRectMake(buttinX, buttinY, buttinW, buttinH);
        index++;
    }
}


- (void)clickAddButton:(UIButton *)sender{
    
    if (!_ISShowMenuButton) {
        
        [UIView animateWithDuration:0.2 animations:^{
            CGAffineTransform rotate = CGAffineTransformMakeRotation( M_PI / 2 );
            [sender setTransform:rotate];
        }];
        [_tlMenuView showItems];
        
    }else{
        
        [UIView animateWithDuration:0.2 animations:^{
            CGAffineTransform rotate = CGAffineTransformMakeRotation( 0 );
            [sender setTransform:rotate];
        }];
        [_tlMenuView dismiss];
    }
    _ISShowMenuButton = !_ISShowMenuButton;
    
}

- (void)buttonTransform {
    
    CGAffineTransform rotate = CGAffineTransformMakeRotation( 0 );
    [UIView animateWithDuration:0.2 animations:^{
        
        [[XMTabBer buttonWithTransform].publishButton setTransform:rotate];
    }];
    [[XMView standardMenuView] dismiss];
    [XMTabBer buttonWithTransform].ISShowMenuButton = NO;
    
}


@end
